package com.springboot.idempotence.redis.aop;

import com.alibaba.fastjson.JSON;
import com.springboot.idempotence.redis.TokenService;
import com.springboot.idempotence.redis.exception.TokenException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @auther: bald ass
 * @date: 2020/5/28 11:59
 */
@Aspect
@Component
public class IdempotentAspect {
    @Autowired
    private TokenService tokenService;

    @Pointcut("execution(public * com.springboot.idempotence.busi.*Controller.*(..))&&args(request)")
    public void idempotent(HttpServletRequest request) {
    }

    @Around("@annotation(com.springboot.idempotence.redis.AutoIdempotent) ")
    public Object doIdempotent(ProceedingJoinPoint joinPoint) {
        try {
            System.out.println("进入AOP断点");
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            //AOP代理类的信息
            Object aThis = joinPoint.getThis();
            //代理的目标对象
            Object target = joinPoint.getTarget();
            //用的最多 通知的签名
            Signature signature = joinPoint.getSignature();
            System.out.println("代理的是方法:" + signature.getName());
            System.out.println("AOP代理类的名字是:" + signature.getDeclaringTypeName());
            //AOP代理类的类（class）信息
            signature.getDeclaringType();
            //获取RequestAttributes
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            //获取RequestAttributes中获取HttpServletRequest的信息
            HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
            System.out.println("请求者IP:" + request.getRemoteAddr());
            boolean flag = tokenService.checkToken(request);
            System.out.println("Token验证结果为:" + flag);
            //如果要获取Session信息的话，可以这样写：
            //HttpSession session = (HttpSession) requestAttributes.resolveReference(RequestAttributes.REFERENCE_SESSION);
            Enumeration<String> enumeration = request.getParameterNames();
            Map<String, String> parameterMap = new HashMap<String, String>();
            while (enumeration.hasMoreElements()) {
                String parameter = enumeration.nextElement();
                parameterMap.put(parameter, request.getParameter(parameter));
            }
            String str = JSON.toJSONString(parameterMap);
            System.out.println("请求的参数信息为：" + str);
            return joinPoint.proceed();
        } catch (TokenException e) {
            return e.getMessage();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("ERR");
            return throwable.getMessage();
        }
    }

}
